package org.example.admin.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.formula.functions.T;
import org.example.admin.annotation.OperationLog;
import org.example.admin.mapper.ItemMapper;
import org.example.admin.service.GoodsService;
import org.example.admin.service.ItemService;
import org.example.entity.GoodsAndItem;
import org.example.entity.Item;
import org.example.entity.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/goods")
@Controller
public class GoodsItemController {

    private ItemService itemService;
    @Resource
    private ItemMapper itemMapper;

    @Resource
    private GoodsService goodsService;

    public GoodsItemController(ItemService itemService) {
        this.itemService = itemService;
    }

    //添加商品种类
    @OperationLog(value = "添加商品种类",requireParamInfo = true)
    @ResponseBody
    @PutMapping("/item/{Id}/{goodName}/{enName}")
    public Object insertGOodsItem(@PathVariable("Id") Integer id,
                                  @PathVariable("goodName") String goodName,
                                  @PathVariable("enName") String enName){
        Item item = new Item();
        item.setId(id);
        item.setName(goodName);
        item.setAlias(enName);
        HashMap<String, Object> map = new HashMap<>();
        try {
            int i = itemService.insertIntem(item);
            if (i==1){
                map.put("code",1);

            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
        }
        return map;
    }


    //查询全部商品
    @ResponseBody
    @GetMapping("/getItem")
    public TableData<Item> getItemPage(Integer offset,Integer limit){
//        System.out.println("---");
        List<Item> itemAll = itemService.getItemAll(offset, limit);
        int size = itemAll.size();
//        System.out.println("size::"+size);

        Page<Item> page = PageHelper.offsetPage(offset, limit);

        TableData<Item> itemTableData = new TableData<>();

        itemTableData.setTotal(size);
        itemTableData.setRows(itemAll);

//        System.out.println(itemTableData);

        return itemTableData;
    }
    //查询商品种类
    @ResponseBody
    @GetMapping("/adminItem")
    public Object selectItem(){
//        System.out.println("查询商品种类");
        return itemMapper.selectList(null);
    }

    //商品分页展示
    @ResponseBody
    @GetMapping("/goodsShow")
    public Object selectAllGoods(Integer offset,Integer limit){
        List<GoodsAndItem> goodsAndItems = goodsService.selectGoodsAll(offset,limit);
        TableData<GoodsAndItem> goodsAndItemTableData = new TableData<>();
        goodsAndItemTableData.setRows(goodsAndItems);
        goodsAndItemTableData.setTotal(goodsService.getGoodsTotal());
        return goodsAndItemTableData;
    }

    //修改商品状态
    @ResponseBody
    @GetMapping("/goodsAlter/{goodsId}/{yang}")
    public Object updateGoodsStatus(@PathVariable("goodsId") Integer id,@PathVariable("yang") Integer status){
        int i = goodsService.updateGoodsStatus(id, status);
//        Map<String, Object> map = new HashMap<>();
        System.out.println("i:"+i);
       return i==1?0:1;
    }
}
